package com.jky.trial.controller;

import com.jky.trial.common.BaseResult;
import com.jky.trial.common.ListResult;
import com.jky.trial.entity.Relation;
import com.jky.trial.service.RelationService;
import com.jky.trial.vo.RelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/dr")
public class RelationController {

    private RelationService recordService;

    @Autowired
    RelationController(RelationService recordService) {
        this.recordService = recordService;
    }

    @PostMapping
    public BaseResult create(@RequestBody Relation record) {
        recordService.create(record);
        return new BaseResult(true, record.getId());
    }

    @PutMapping
    public BaseResult update(@RequestBody Relation record) {
        return new BaseResult(true, recordService.update(record));
    }

    /**
     * 更新关系
     * record.type 关系类型
     * record.targetIds 关系目标Id
     * record.sourceId 关系源Id
     * @param record
     * @return
     */
    @PutMapping("updateBatch")
    public BaseResult updateRelation(@RequestBody RelationVo record) {
        return new BaseResult(true, recordService.updateRelation(record));
    }

    @DeleteMapping("/{id}")
    public BaseResult del(@PathVariable Integer id) {
        return new BaseResult(true, recordService.del(id));
    }

    /**
     * 通过sourceId查找个关系
     * @param sourceId //目标Id
     * @param type      //类型
     * @return
     */
    @GetMapping("/source/{sourceId}/{type}")
    public BaseResult drBySource(@PathVariable Integer sourceId,@PathVariable Integer type) {
        Relation record = new Relation();
        record.setType(type);
        record.setSourceId(sourceId);
        return new ListResult(true, recordService.list(record));
    }

    /***
     * 根据targetId查找关系
     * @param targetId
     * @param type
     * @return
     */
    @GetMapping("/targetId/{targetId}/{type}")
    public BaseResult drByTarget(@PathVariable Integer targetId,@PathVariable Integer type) {
        Relation record = new Relation();
        record.setType(type);
        record.setTargetId(targetId);
        return new ListResult(true, recordService.list(record));
    }
    @GetMapping("/type/{type}")
    public BaseResult drByType(@PathVariable Integer type) {
        Relation record = new Relation();
        record.setType(type);
        return new ListResult(true, recordService.list(record));
    }


}
